home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / WarriorsProgress.sit / Warrior’s Progress / source code / Source / Libraries / Files / CatInfo.cp next >
Text File  |  1997-06-28  |  4KB  |  201 lines

  1. // CatInfo.cp
  2.  
  3. #ifndef CatInfo_h
  4. #include "CatInfo.h"
  5. #endif
  6. #ifndef FileNotFoundError_h
  7. #include "FileNotFoundError.h"
  8. #endif
  9. #ifndef DirectoryNotFoundError_h
  10. #include "DirectoryNotFoundError.h"
  11. #endif
  12. #ifndef HardwareVolumeLockError_h
  13. #include "HardwareVolumeLockError.h"
  14. #endif
  15. #ifndef SoftwareVolumeLockError_h
  16. #include "SoftwareVolumeLockError.h"
  17. #endif
  18. #ifndef FileLockError_h
  19. #include "FileLockError.h"
  20. #endif
  21. #ifndef FilePermissionError_h
  22. #include "FilePermissionError.h"
  23. #endif
  24. #ifndef NotADirectoryError_h
  25. #include "NotADirectoryError.h"
  26. #endif
  27.  
  28. CatInfo::CatInfo()
  29.   {
  30.     hFileInfo.ioCompletion = 0;
  31.     hFileInfo.ioNamePtr = location.name;
  32.     hFileInfo.ioFVersNum = 0;
  33.   }
  34.  
  35. CatInfo::CatInfo( const FileLocation& target )
  36.   {
  37.     hFileInfo.ioCompletion = 0;
  38.     hFileInfo.ioNamePtr = location.name;
  39.     hFileInfo.ioFVersNum = 0;
  40.     Get( target );
  41.   }
  42.  
  43. CatInfo::CatInfo( ::Directory directory )
  44.   {
  45.     hFileInfo.ioCompletion = 0;
  46.     hFileInfo.ioNamePtr = location.name;
  47.     hFileInfo.ioFVersNum = 0;
  48.     Get( directory );
  49.   }
  50.  
  51. CatInfo::CatInfo( ::Directory directory, ConstPString name )
  52.   {
  53.     hFileInfo.ioCompletion = 0;
  54.     hFileInfo.ioNamePtr = location.name;
  55.     hFileInfo.ioFVersNum = 0;
  56.     Get( directory, name );
  57.   }
  58.  
  59. CatInfo::CatInfo( ::Directory directory, int16 index )
  60.   {
  61.     hFileInfo.ioCompletion = 0;
  62.     hFileInfo.ioNamePtr = location.name;
  63.     hFileInfo.ioFVersNum = 0;
  64.     Get( directory, index );
  65.   }
  66.  
  67. CatInfo::CatInfo( const CatInfo& source )
  68.   : CInfoPBRec( source ),
  69.      location( source.location )
  70.   {
  71.     hFileInfo.ioNamePtr = location.name;
  72.   }
  73.  
  74. void CatInfo::operator=( const CatInfo& source )
  75.   {
  76.     static_cast< CInfoPBRec& >( *this ) = source;
  77.     location = source.location;
  78.     hFileInfo.ioNamePtr = location.name;
  79.   }
  80.  
  81. void CatInfo::Get()
  82.   {
  83.     hFileInfo.ioVRefNum = location.Volume().RefNum();
  84.     hFileInfo.ioDirID = location.ParentID().Number();
  85.     hFileInfo.ioFDirIndex = 0;
  86.     ThrowError( PBGetCatInfoSync( this ) );
  87.     
  88.     Assert( hFileInfo.ioVRefNum == location.vRefNum );
  89.     Assert( hFileInfo.ioFlParID == location.parID );
  90.   }
  91.  
  92. void CatInfo::Get( const FileLocation& target )
  93.   {
  94.     location = target;
  95.     Get();
  96.   }
  97.  
  98. void CatInfo::Get( ::Directory directory )
  99.   {
  100.     hFileInfo.ioVRefNum = directory.Volume().RefNum();
  101.     hFileInfo.ioDirID = directory.ID().Number();
  102.     hFileInfo.ioFDirIndex = -1;
  103.     ThrowError( PBGetCatInfoSync( this ) );
  104.     
  105.     location.vRefNum = hFileInfo.ioVRefNum;
  106.     location.parID = hFileInfo.ioFlParID;
  107.   }
  108.  
  109. void CatInfo::Get( ::Directory directory, ConstPString name )
  110.   {
  111.     location.Set( directory, name );
  112.     Get();
  113.   }
  114.  
  115. void CatInfo::Get( ::Directory directory, int16 index )
  116.   {
  117.     Assert( index > 0 );
  118.     hFileInfo.ioVRefNum = directory.Volume().RefNum();
  119.     hFileInfo.ioDirID = directory.ID().Number();
  120.     hFileInfo.ioFDirIndex = index;
  121.     ThrowError( PBGetCatInfoSync( this ) );
  122.  
  123.     location.vRefNum = hFileInfo.ioVRefNum;
  124.     location.parID = hFileInfo.ioFlParID;
  125.     Assert( directory.Volume().RefNum() == location.vRefNum );
  126.     Assert( directory.ID().Number() == location.parID );
  127.   }
  128.  
  129. void CatInfo::Set()
  130.   {
  131.     hFileInfo.ioVRefNum = location.Volume().RefNum();
  132.     hFileInfo.ioDirID = location.ParentID().Number();
  133.     ThrowError( PBSetCatInfoSync( this ) );
  134.   }
  135.  
  136. void CatInfo::Set( const FileLocation& target )
  137.   {
  138.     location = target;
  139.     Set();
  140.   }
  141.  
  142. ::Directory CatInfo::AsDirectory() const
  143.   {
  144.     if ( !IsDirectory() )
  145.         throw NotADirectoryError( noErr );
  146.     
  147.     return ::Directory( location.Volume(), Directory().ID() );
  148.   }
  149.  
  150. void CatInfo::Up()
  151.   {
  152.     Assert( !IsRoot() );
  153.     if ( IsRoot() )
  154.         ThrowError( dirNFErr );
  155.  
  156.     Get( location.Parent() );
  157.   }
  158.  
  159. void CatInfo::Down( ConstPString child )
  160.   {
  161.     Assert( IsDirectory() );
  162.     if ( !IsDirectory() )
  163.         throw NotADirectoryError( noErr );
  164.  
  165.     location.SetParentID( Directory().ID() );
  166.     location.SetName( child );
  167.     Get();
  168.   }
  169.  
  170. void CatInfo::Sideways( ConstPString sibling )
  171.   {
  172.     location.SetName( sibling );
  173.     Get();
  174.   }
  175.  
  176. void CatInfo::SetLabel( uint8 label )
  177.   {
  178.     Assert( label < 8 );
  179.     hFileInfo.ioFlFndrInfo.fdFlags =
  180.          ( hFileInfo.ioFlFndrInfo.fdFlags & ~kColor )
  181.       | ( label << 1 );
  182.   }
  183.  
  184. void CatInfo::ThrowError( OSErr error )
  185.   {
  186.     if ( error == noErr )
  187.         return;
  188.     
  189.     switch ( error )
  190.       {
  191.         case fnfErr:                throw FileNotFoundError( error );
  192.         case dirNFErr:                throw DirectoryNotFoundError( error );
  193.         case wPrErr:                throw HardwareVolumeLockError( error );
  194.         case vLckdErr:                throw SoftwareVolumeLockError( error );
  195.         case fLckdErr:                throw FileLockError( error );
  196.         case afpAccessDenied:    throw FilePermissionError( error );
  197.       }
  198.     
  199.     throw FileError( error );
  200.   }
  201.